package com.ruoyi.mdkt.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.mdkt.entity.*;
import com.ruoyi.mdkt.entity.vo.CourseVo;
import com.ruoyi.mdkt.mapper.CourseMapper;
import com.ruoyi.mdkt.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.system.service.ISysUserService;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author gwl
 * @since 2022-10-22
 */
@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements CourseService {
    @Autowired
    private CourseService courseService;
    @Autowired
    private CourseStudentService courseStudentService;
    @Autowired
    private CourseDateService courseDateService;
    @Autowired
    private TaskService taskService;

    @Autowired
    private TeacherService teacherService;

    @Autowired
    private ISysUserService iSysUserService;
    @Autowired
    private CourseInfoService courseInfoService;
    @Override
    public Page<Course> pageByTeacherUserId(Long teacherUserId,Integer pageSize, Integer pageNum) {
        Page<Course> page = new Page<>(pageNum,pageSize);
        LambdaQueryWrapper<Course> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Course::getTeacherId,teacherUserId);
        courseService.page(page,wrapper);
        return page;
    }

    @Override
    public Course getClassing(Long userId) {
        LambdaQueryWrapper<CourseStudent> courseStudentWrapper = new LambdaQueryWrapper<>();
        courseStudentWrapper.eq(CourseStudent::getStudentUserId,userId);
        List<CourseStudent> courseStudentList = courseStudentService.list(courseStudentWrapper);
        if (courseStudentList != null){
            return null;
        }
        List<Long> ids = new ArrayList<>();
        if (courseStudentList.size() == 0){
            return null;
        }
        for (CourseStudent c:
             courseStudentList) {
            ids.add(c.getCourseId());
        }
        LambdaQueryWrapper<CourseDate> courseDateLambdaQueryWrapper = new LambdaQueryWrapper<>();
        courseDateLambdaQueryWrapper.in(CourseDate::getCourseId,ids);
        courseDateLambdaQueryWrapper.le(CourseDate::getStartTime,LocalDateTime.now());
        courseDateLambdaQueryWrapper.ge(CourseDate::getEndTime,LocalDateTime.now());
        CourseDate one = courseDateService.getOne(courseDateLambdaQueryWrapper);
        if (one == null){
            return null;
        }
        Course byId = courseService.getById(one.getCourseId());
        if (byId == null){
            return null;
        }
        return byId;
    }

    @Override
    public List<Course> getNotClassing(Long userId) {
        LambdaQueryWrapper<CourseStudent> courseStudentWrapper = new LambdaQueryWrapper<>();
        courseStudentWrapper.eq(CourseStudent::getStudentUserId,userId);
        List<CourseStudent> courseStudentList = courseStudentService.list(courseStudentWrapper);
        if (courseStudentList == null){
            return null;
        }
        List<Long> ids = new ArrayList<>();
        for (CourseStudent c:
                courseStudentList) {
            ids.add(c.getCourseId());
        }
        LambdaQueryWrapper<CourseDate> courseDateLambdaQueryWrapper = new LambdaQueryWrapper<>();
        courseDateLambdaQueryWrapper.in(CourseDate::getCourseId,ids);
        courseDateLambdaQueryWrapper.gt(CourseDate::getStartTime,LocalDateTime.now());
        courseDateLambdaQueryWrapper.or();
        courseDateLambdaQueryWrapper.lt(CourseDate::getEndTime,LocalDateTime.now());
        List<CourseDate> list = courseDateService.list(courseDateLambdaQueryWrapper);
        List<Long> nIds = new ArrayList<>();
        list.stream().forEach((ites)->{
            nIds.add(ites.getCourseId());
        });
        LambdaQueryWrapper<Course> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(Course::getId,nIds);
        List<Course> courses = courseService.list(wrapper);
        return courses;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void publishCourse(Long courseId) {
        Course byId = courseService.getById(courseId);
        Task task = new Task();
        task.setCourseId(courseId);
        task.setTeacherUserId(SecurityUtils.getUserId());
        task.setTaskName("平时成绩");
        task.setType(1);
        taskService.save(task);
        byId.setType(1);
        courseService.updateById(byId);
    }

    @Override
    public Page<CourseVo> pageList(Course course, int pageNum, int pageSize) {
        LambdaQueryWrapper<Course> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(course.getCourseName()!= null,Course::getCourseName,course.getCourseName());
        wrapper.eq(course.getSemester() != null,Course::getSemester,course.getSemester());
        Page<Course> coursePage = new Page<>(pageNum,pageSize);
        Page<CourseVo> courseVoPage = new Page<>(pageNum,pageSize);
        courseService.page(coursePage,wrapper);
        BeanUtils.copyProperties(coursePage,courseVoPage,"records");
        List<Course> records = coursePage.getRecords();
        List<CourseVo> collect = records.stream().map((item) -> {
            CourseVo courseVo = new CourseVo();
            BeanUtils.copyProperties(item, courseVo);
            courseVo.setTeacherName(getTeacherName(item.getTeacherId()));
            courseVo.setCourseInfo(getCourseInfo(item.getId()));
            return courseVo;
        }).collect(Collectors.toList());
        courseVoPage.setRecords(collect);
        return courseVoPage;
    }

    private String getCourseInfo(Long id) {
        LambdaQueryWrapper<CourseInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CourseInfo::getCourseId,id);
        CourseInfo one = courseInfoService.getOne(wrapper);
        if (one == null){
            return null;
        }
        return one.getInfo();
    }

    private String getTeacherName(Long teacherId) {
        SysUser sysUser = iSysUserService.selectUserById(teacherId);

        return sysUser.getNickName();
    }


}
